package com.vy.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vy.stl.entity.Member;
import com.vy.stl.entity.Reserve;
import com.vy.stl.mapper.MemberMapper;
import com.vy.stl.mapper.ReserveMapper;
import com.vy.stl.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

/**
 * @create: 2025/08/29 13:50
 * @description:
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ReserveMapper reserveMapper;

    @Override
    public List<Member> search(Member member) {
        return memberMapper.select(member);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Member member) {
        if (ObjectUtils.isEmpty(page)) page = 1;
        if (ObjectUtils.isEmpty(limit)) limit = 10;
        // 开启分页查询
        PageHelper.startPage(page, limit);
        List<Member> members = memberMapper.select(member);
        PageInfo<Member> pageInfo = new PageInfo<Member>(members);
        return pageInfo;
    }

    @Override
    public int add(Member member) throws Exception {
        Member dbmember = memberMapper.selectByPhone(member.getPhone());
        if (!ObjectUtils.isEmpty(dbmember)) {
            throw new Exception("手机号被占用，无法注册");
        }
        // 对明文密码进行加密
        //***前后端都用手机号盐值加密有点危险
        String md5Password = getMd5Password(member.getPassword(), member.getPhone());
        member.setPassword(md5Password);
        member.setCreateTime(new Date());
        return memberMapper.insert(member);
    }

    @Override
    public int edit(Member member) throws Exception {
        //1.判断修改的会员是否存在
        Member hasMember = memberMapper.selectByParimaryKey(member.getId());
        if (ObjectUtils.isEmpty(hasMember)) {
            throw new Exception("会员不存在，删除失败");
        }
        Member dbMember = memberMapper.selectByPhone(member.getPhone());
        if (!ObjectUtils.isEmpty(dbMember) && dbMember.getId() != member.getId()) {
            throw new Exception("手机号不能重复");
        }
        String md5Password = getMd5Password(member.getPassword(), member.getPhone());
        member.setPassword(md5Password);
        return memberMapper.edit(member);
    }

    @Override
    public int remove(Integer id) throws Exception {
        // 前提1：必须保证被删除的会员存在
        Member member = memberMapper.selectByParimaryKey(id);
        if (ObjectUtils.isEmpty(member)) {
            throw new Exception("会员不存在，无法删除");
        }
        // 前提2：必须保证该会员没有任何预约的课程
        // 根据当前会员的id，查询预约表（返回List<Reserve>)，如果能查到数据，说明该会员已经有预约的课程了，无法删除，反之可以删除
        List<Reserve> reserves = reserveMapper.selectByMemberId(id);
        if (!reserves.isEmpty()) {
            throw new Exception("该会员还有未完成的课程，无法删除");
        }
        //没问题，可以删除
        return memberMapper.deleteById(id);
    }

//    @Override
//    public Member getByPhone(String phone) {
//        return memberMapper.selectByPhone(phone);
//    }
    @Override
    public Member getMemberByPhone(String phone) {
        return memberMapper.selectByPhone(phone);
    }

    /**
     * 前台登录
     * @param member
     * @return
     * @throws Exception
     */
    @Override
    public Member login(Member member) throws Exception {
        //验证手机号
        Member loginMember = memberMapper.selectByPhone(member.getPhone());
        if (ObjectUtils.isEmpty(loginMember)){
            throw new Exception("手机号不存在！");
        }
        //验证密码
        String md5Password = getMd5Password(member.getPassword(),member.getPhone());
        if (!md5Password.equals(loginMember.getPassword())){
            throw new Exception("密码不正确");
        }

        return loginMember;
    }

    private String getMd5Password(String password, String salt) {
        for (int i = 0; i < 3; i++) {
            String newPass = salt + password + salt;
            password = DigestUtils.md5DigestAsHex(newPass.getBytes()).toUpperCase();
        }
        return password;
    }
}
